package com.itCar.base.api.file.service.impl;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import com.itCar.base.api.file.service.FileService;
import com.itCar.base.config.exceptionhandler.BizException;
import com.itCar.base.tools.DateUtil;
import com.itCar.base.tools.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName: FileServiceImpl
 * @Description: TODO
 * @author: liuzg
 * @Date: 2022/9/14 14:34
 * @Week: 星期三
 * @Version: v1.0
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    //根据不同系统环境获取不同的文件层次符
    private static final String FILE_SEPARATORS = File.separator;

    @Value("${config.fi.plate}")
    private String plate;
    @Value("${config.fi.uploadFile}")
    private String directory;



    @Override
    public String fileCovert(MultipartFile file) {
        String path = "";
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        if ("pdf".equals(suffix)){
            System.out.println("=== pdf 转 word 开始 ===");
            path = pw(file);
            System.out.println("=== pdf 转 word 结束 ===");
        }else if ("docx".equals(suffix) || "doc".equals(suffix)){
            System.out.println("=== word 转 pdf 开始 ===");
            path = wp(file);
            System.out.println("=== word 转 pdf 结束 ===");
        }else {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "文件类型错误，仅支持PDF、WORD");
        }
        return path;
    }


    @Override
    public String pw(MultipartFile file) {
        String pdfPath = FileUtils.checkFile(file);

        String wordPath = "";
        String old = DateUtil.getDate(0);
        try {
            //新建一个word文档
            String name = pdfPath.substring(pdfPath.lastIndexOf(FILE_SEPARATORS) + 1, pdfPath.lastIndexOf("."));
            wordPath = plate + ":" + FILE_SEPARATORS + directory + FILE_SEPARATORS + name + ".docx";
            FileOutputStream os = new FileOutputStream(wordPath);
            //doc是将要被转化的word文档
            Document doc = new Document(pdfPath);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.DocX);
            os.close();
            //转化用时
            System.out.println("Pdf 转 Word 共耗时：" + DateUtil.getTimeDifference(DateUtil.getDate(0), old));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "pdf 转 word 失败...");
        }//finally { //删除刚上传的文件
//            FileUtils.asyncDel(pdfPath);
//        }
        return wordPath;
    }


    @Override
    public String wp(MultipartFile file) {
        String wordPath = FileUtils.checkFile(file);

        String pdfPath = "";
        String old = DateUtil.getDate(0);
        try {
            //新建一个pdf文档
            String name = wordPath.substring(wordPath.lastIndexOf(FILE_SEPARATORS) + 1, wordPath.lastIndexOf("."));
            pdfPath = plate + ":" + FILE_SEPARATORS + directory + FILE_SEPARATORS + name + ".pdf";
            FileOutputStream os = new FileOutputStream(pdfPath);
            com.aspose.words.Document doc = new com.aspose.words.Document(wordPath); //Address是将要被转化的word文档
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            os.close();
            //转化用时
            System.out.println("word 转 pdf 共耗时：" + DateUtil.getTimeDifference(DateUtil.getDate(0), old));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "pdf 转 word 失败...");
        }
        return pdfPath;
    }


}
